package com.hotel.homehotel.before.controller;

import com.hotel.homehotel.before.entiy.Notice;
import com.hotel.homehotel.before.service.NoticeService;
import com.hotel.homehotel.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
@Api(tags="新闻管理")
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    @ApiOperation(value = "获取所有新闻")
    @PostMapping("/getAllNews")
    public R getAllNews() {

       return R.define(true,200,"获取成功").data("news",noticeService.getAllNews());
    }


    @ApiOperation(value = "获取所有轮播图")
    @PostMapping("/getRim")
    public R getRim() {

        return R.define(true,200,"获取成功").data("news",noticeService.getRim());
    }
    @ApiOperation(value = "通过id获取通知信息")  //包括轮播图和新闻
    @GetMapping("/getNoticeById/{id}")
    public R getNoticeById(@ApiParam(name = "id", value = "通知id", required = true)
                               @PathVariable     String id){
          System.out.println(id+"kkkkkkkkkkkkkkkkkkkkkkkkkkk");
      return R.define(true,200,"获取成功").data("notice",noticeService.getNoticeById(id));
    }

    @ApiOperation(value = "删除通知")  //包括轮播图和新闻
    @PostMapping("/delete/{id}")
    public   R delete(@ApiParam(name = "id", value = "通知id", required = true)
                          @PathVariable     String id){
        boolean delete = noticeService.delete(id);
        if(delete)
            return  R.define(true,200,"删除成功");
        return  R.define(false,400,"删除失败");
    }
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "添加通知")  //包括轮播图和新闻
    @PostMapping("/insert")
    public   R insert( @RequestBody Notice notice){
        boolean insert = noticeService.insert(notice);
        if(insert)
            return  R.define(true,200,"添加成功");
        return  R.define(false,400,"添加失败");
    }
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "修改通知")  //包括轮播图和新闻
    @PostMapping("/update")
    public   R update( @RequestBody Notice notice){
        boolean update = noticeService.update(notice);
        if(update)
            return  R.define(true,200,"删除成功");
        return  R.define(false,400,"删除失败");
    }

}
